(defun c:SimpleStack (/ ref-ss layer-list filter ss i ent obj textstring newtext hashpos newlinepos numerator denominator found hasNewline)
  (vl-load-com)
  (princ "\n=== 开始水平堆叠转换（按图层处理）===\n")
  
  (princ "步骤1: 选择参考对象以确定图层... ")
  (setq ref-ss (ssget))
  (if (not ref-ss)
    (progn
      (princ "失败 - 未选择到对象\n")
      (princ "=== 结束 ===\n")
      (princ)
    )
    (progn
      (princ (strcat "成功 - 选择了 " (itoa (sslength ref-ss)) " 个参考对象\n"))
      
      ; 收集所有不重复的图层名
      (setq layer-list '())
      (setq i 0)
      (repeat (sslength ref-ss)
        (setq ent (ssname ref-ss i))
        (if ent
          (progn
            (setq layer-name (cdr (assoc 8 (entget ent))))
            (if (not (member layer-name layer-list))
              (setq layer-list (cons layer-name layer-list))
            )
          )
        )
        (setq i (1+ i))
      )
      
      (princ (strcat "步骤2: 找到 " (itoa (length layer-list)) " 个图层: " (apply 'strcat (mapcar '(lambda (x) (strcat x " ")) layer-list)) "\n"))
      
      ; 构建过滤器来选择指定图层上的所有多行文字
      (princ "步骤3: 选择指定图层上的所有多行文字... ")
      (if (= (length layer-list) 1)
        (setq filter (list '(0 . "MTEXT") (cons 8 (car layer-list))))
        (setq filter (list '(0 . "MTEXT") (cons 8 (apply 'strcat (mapcar '(lambda (x) (strcat x ",")) (reverse (cdr (reverse layer-list)))) (car (last layer-list))))))
      )
      
      (setq ss (ssget "_X" filter))
      (if (not ss)
        (progn
          (princ "失败 - 在指定图层上未找到多行文字对象\n")
          (princ "=== 结束 ===\n")
          (princ)
        )
        (progn
          (princ (strcat "成功 - 选择了 " (itoa (sslength ss)) " 个多行文字对象\n"))
          
          (setq i 0)
          (repeat (sslength ss)
            (princ (strcat "\n处理第 " (itoa (1+ i)) " 个对象:\n"))
            
            (princ "步骤4: 获取图元... ")
            (setq ent (ssname ss i))
            (if (not ent)
              (princ "失败 - 图元为空\n")
              (progn
                (princ "成功\n")
                
                (princ "步骤5: 转换为VLA对象... ")
                (setq obj (vlax-ename->vla-object ent))
                (if (not obj)
                  (princ "失败\n")
                  (progn
                    (princ "成功\n")
                    
                    (princ "步骤6: 获取文字内容... ")
                    (setq textstring (vla-get-textstring obj))
                    (if (not textstring)
                      (princ "失败 - 文字内容为空\n")
                      (progn
                        (princ (strcat "成功 - 内容: " textstring "\n"))
                        
                        (setq found nil)
                        (setq hasNewline nil)
                        
                        ; 检查是否包含#符号
                        (princ "步骤7: 检查是否包含#符号... ")
                        (setq hashpos (vl-string-search "#" textstring))
                        (if hashpos
                          (progn
                            (princ (strcat "是 - 位置: " (itoa hashpos) "\n"))
                            (princ "步骤8: 提取分子和分母... ")
                            ; 提取#前面的分子
                            (setq numerator (substr textstring 1 hashpos))
                            ; 提取#后面的分母
                            (setq denominator (substr textstring (+ hashpos 2)))
                            (setq found T)
                          )
                          (progn
                            (princ "否\n")
                            ; 检查是否包含实际的换行符（ASCII 10）
                            (princ "步骤7a: 检查是否包含换行符(ASCII 10)... ")
                            (setq newlinepos (vl-string-position 10 textstring))
                            (if newlinepos
                              (progn
                                (princ (strcat "是 - 位置: " (itoa newlinepos) "\n"))
                                (princ "步骤8a: 提取分子和分母... ")
                                ; 提取换行符前面的分子
                                (setq numerator (substr textstring 1 newlinepos))
                                ; 提取换行符后面的分母（跳过换行符）
                                (setq denominator (substr textstring (+ newlinepos 2)))
                                (setq found T)
                                (setq hasNewline T)
                              )
                              (progn
                                (princ "否\n")
                                ; 检查是否包含回车符（ASCII 13）
                                (princ "步骤7b: 检查是否包含回车符(ASCII 13)... ")
                                (setq newlinepos (vl-string-position 13 textstring))
                                (if newlinepos
                                  (progn
                                    (princ (strcat "是 - 位置: " (itoa newlinepos) "\n"))
                                    (princ "步骤8b: 提取分子和分母... ")
                                    ; 提取回车符前面的分子
                                    (setq numerator (substr textstring 1 newlinepos))
                                    ; 提取回车符后面的分母（跳过回车符）
                                    (setq denominator (substr textstring (+ newlinepos 2)))
                                    (setq found T)
                                    (setq hasNewline T)
                                  )
                                  (progn
                                    (princ "否\n")
                                    ; 检查是否包含"\P"文本
                                    (princ "步骤7c: 检查是否包含\\P文本... ")
                                    (setq newlinepos (vl-string-search "\\P" textstring))
                                    (if newlinepos
                                      (progn
                                        (princ (strcat "是 - 位置: " (itoa newlinepos) "\n"))
                                        (princ "步骤8c: 提取分子和分母... ")
                                        ; 提取\P前面的分子
                                        (setq numerator (substr textstring 1 newlinepos))
                                        ; 提取\P后面的分母（跳过\P的两个字符）
                                        (setq denominator (substr textstring (+ newlinepos 3)))
                                        (setq found T)
                                      )
                                      (princ "否 - 跳过\n")
                                    )
                                  )
                                )
                              )
                            )
                          )
                        )
                        
                        (if found
                          (progn
                            (princ (strcat "成功 - 分子: " numerator " 分母: " denominator "\n"))
                            
                            (princ "步骤9: 构建堆叠格式... ")
                            (setq newtext (strcat "\\A1;\\S" numerator "/" denominator ";"))
                            (princ (strcat "成功 - 新内容: " newtext "\n"))
                            
                            (princ "步骤10: 更新文字对象... ")
                            (vla-put-textstring obj newtext)
                            (princ "成功\n")
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
            (setq i (1+ i))
          )
        )
      )
    )
  )
  
  (princ "\n=== 转换完成 ===\n")
  (princ)
)

; 简化命令
(defun c:BZ () (c:SimpleStack))

(princ "\n按图层处理的简单堆叠程序已加载，输入 bz 或 BZ 使用\n")
(princ)